#!/bin/bash
set -ex  # 显示命令并遇到错误立即退出
rm -rf *
ANDROID_NDK=/mnt/android_home/ndk/r21e
TOOLCHAIN=$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64

CPU=armv8-a
API=21
HOST=armv7a-linux-androideabi
ARCH=aarch64

export CC=$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang
export CXX=$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang++
#export AR=$TOOLCHAIN/bin/llvm-ar
#export RANLIB=$TOOLCHAIN/bin/llvm-ranlib
#export STRIP=$TOOLCHAIN/bin/llvm-strip

SYSROOT=$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot
export CROSS_PREFIX=$TOOLCHAIN/bin/llvm-
PREFIX=$(pwd)/android/$CPU
OPTIMIZE_CFLAGS="-march=$CPU"
EXTRA_LDFLAGS="-Wl,-z,max-page-size=65536 -Wl,-z,common-page-size=65536"

# 🔧 关键修复：创建符号链接，让 configure 能找到需要的工具
#ln -sf $TOOLCHAIN/bin/llvm-strings    $TOOLCHAIN/bin/aarch64-linux-android-strings
#ln -sf $TOOLCHAIN/bin/llvm-readelf    $TOOLCHAIN/bin/aarch64-linux-android-readelf
#ln -sf $TOOLCHAIN/bin/llvm-ar         $TOOLCHAIN/bin/aarch64-linux-android-ar
#ln -sf $TOOLCHAIN/bin/llvm-ranlib     $TOOLCHAIN/bin/aarch64-linux-android-ranlib
#ln -sf $TOOLCHAIN/bin/llvm-strip      $TOOLCHAIN/bin/aarch64-linux-android-strip

# 进入源码目录
cd /opt/module/src/x264/master

# ✅ 运行 configure
/opt/module/src/x264/master/configure \
  --disable-opencl \
  --disable-ffms \
  --disable-gpac \
  --disable-lavf \
  --disable-swscale \
  --disable-lsmash \
  --disable-cli \
  --disable-asm \
  --enable-static \
  --enable-debug \
  --enable-pic \
  --prefix=$PREFIX \
  --host=$HOST \
  --cross-prefix=$CROSS_PREFIX \
  --sysroot=$SYSROOT
 # --extra-cflags="-Os -fpic $OPTIMIZE_CFLAGS" \
 # --extra-ldflags="$EXTRA_LDFLAGS"
 # 编译
 make -j$(nproc)
 # 安装
# make install
